import 'package:flutter/material.dart';
import 'package:icooker/configs/mock_datas.dart';
import 'package:icooker/model/reommend_data.dart';
import 'package:flutter_ijkplayer/flutter_ijkplayer.dart';

/// 基于: AwesomePlayerView
class AwesomePlayerView extends StatefulWidget {
  VideoInfoData videoInfo;

  AwesomePlayerView(this.videoInfo, {Key? key}) : super(key: key);

  @override
  _AwesomePlayerViewState createState() => _AwesomePlayerViewState();
}

class _AwesomePlayerViewState extends State<AwesomePlayerView> {
  IjkMediaController controller = IjkMediaController();
  late Future<void> _initPlayerFuture;

  @override
  void initState() {
    super.initState();
    var videoUrl = URL_HTTP_VIDEOS[3];
    _initPlayerFuture = controller.setNetworkDataSource(
        // widget.videoInfo.video!.vendorVideo!,
        videoUrl,
        autoPlay: true);
  }

  @override
  void dispose() {
    controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: FutureBuilder(
        future: _initPlayerFuture,
        builder: (context, snapshot) {
          if (snapshot.connectionState == ConnectionState.done) {
            return AspectRatio(
              aspectRatio: controller.videoInfo.ratio,
              child: buildIjkPlayer(),
            );
          } else {
            return Center(
              child: CircularProgressIndicator(),
            );
          }
        },
      ),
    );
  }

  IjkPlayer buildIjkPlayer() {
    return IjkPlayer(
      mediaController: controller,
    );
  }
}
